﻿using System;
using System.Collections.Generic;
using System.Text;

namespace TestEffectiveCS
{
    /// <summary>
    /// 明辨值类型和引用类型的使用场合
    /// </summary>
    public class Test06
    {
        public class BankAccount
        {
            decimal _balance = 0;
            public decimal Balance { get { return _balance; } set { _balance = value; } }
        }
        public struct Employee
        {
            private decimal _salary;
            public decimal Salary { get { return _salary; } set { _salary = value; } }
            public void Pay(BankAccount b)
            {
                b.Balance += _salary;
            }
        }
        //public class Employee
        //{
        //    private decimal _salary;
        //    public decimal Salary { get { return _salary; } set { _salary = value; } }
        //    public void Pay(BankAccount b)
        //    {
        //        b.Balance += _salary;
        //    }
        //}
        public static class Employees
        {
            static Employee employee = new Employee();

            //如果Employee是结构体，则是值传递
            //如果Employee是类，则是引用传递
            public static Employee Find() { return employee; }
        }
        public Test06()
        {
            Employee e1 = Employees.Find();
            BankAccount ba1 = new BankAccount();
            e1.Salary += 10;
            e1.Pay(ba1);
            Console.WriteLine(ba1.Balance);

            Employee e2 = Employees.Find();
            BankAccount ba2 = new BankAccount();
            e2.Salary += 10;
            e2.Pay(ba2);
            Console.WriteLine(ba2.Balance);
        }
    }
}